package br.com.vti.sisp_sicrim.antigo.dao;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.UUID;

import br.com.vti.sisp_sicrim.antigo.dao.util.HibernateUtil;

public class SicHisTabelasDAO {

	private int codigoRetorno = 0; // *ERROR
	private String msgRetorno = ""; // *ERROR
	public long qtdRegistros = 0; // *counter
	public long nroRegistro = 0; // *counter
	private String[] listacol = { "", "Descel", "Observacao", "Chalfa", "Grupel", "CodElemento", "DataAtualizacao", "MatriculaFunc", "DataPo", "CodigoTabela" };
	private ResultSet query = null;

	public String insert(String pDescel, String pObservacao, String pChalfa, String pGrupel, Long pCodElemento, Integer pDataAtualizacao, Integer pMatriculaFunc, Integer pDataPo, Integer pCodigoTabela) {

		String sql = "{ ? = call sp_grava_Sic_His_Tabelas ( ?,?,?,?,?,?,?,?,? ) }";
		UUID pId;
		codigoRetorno = 0;
		qtdRegistros = 0;
		nroRegistro = 0;
		msgRetorno = "";
		try {

			CallableStatement stp = HibernateUtil.getConnection().prepareCall(sql);
			stp.registerOutParameter(1, Types.OTHER);
			stp.setString(2, pDescel); // (getColuna("Descel")
			stp.setString(3, pObservacao); // (getColuna("Observacao")
			stp.setString(4, pChalfa); // (getColuna("Chalfa")
			stp.setString(5, pGrupel); // (getColuna("Grupel")
			stp.setLong(6, pCodElemento); // (getColuna("CodElemento")
			stp.setInt(7, pDataAtualizacao); // (getColuna("DataAtualizacao")
			stp.setInt(8, pMatriculaFunc); // (getColuna("MatriculaFunc")
			stp.setInt(9, pDataPo); // (getColuna("DataPo")
			stp.setInt(10, pCodigoTabela); // (getColuna("CodigoTabela")
			// execute SP
			stp.execute();
			pId = (UUID) stp.getObject(1);
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
			return null;
		}
		return String.valueOf(pId);
	}

	public ResultSet pesquisaProximo() {
		// query
		try {
			if (query == null)
				return null;
			if (query.getRow() == 0) {
				query = null;
				return null;
			}
			query.next();
			if (query.getRow() == 0) {
				query = null;
				return null;
			}
			if (query != null) {
				nroRegistro++;
			}
			return query;
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
			return null;
		}
	}

	public boolean temProximo() {
		try {
			if (query == null)
				return false;
			if (query.getRow() == 0) {
				return false;
			}
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
			return false;
		}
		return (nroRegistro < qtdRegistros);
	}

	public void fecha() {
		if (query == null)
			return;
		try {
			query.close();
			query = null;
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
			query = null;
		}
	}

	public int getColuna(String coluna) {
		for (int i = 0; i < listacol.length; i++) {
			if (listacol[i].equals(coluna))
				return i;
		}
		return -1;
	}

}
